package my.edu.model.information.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import my.edu.common.domain.PageDTO;
import my.edu.common.domain.PageVO;
import my.edu.common.utils.PageUtils;
import my.edu.model.User.entity.User;
import my.edu.model.User.entity.VO.OperationLogVO;
import my.edu.model.User.entity.dto.LogSearchDTO;
import org.springframework.stereotype.Service;
import my.edu.model.information.entity.OperationLog;
import my.edu.model.information.mapper.OperationLogMapper;

import java.util.List;

@Service
public class OperationLogServiceImpl extends ServiceImpl<OperationLogMapper, OperationLog> implements IOperationLogService{

    @Override
    public PageVO<OperationLogVO> getList(PageDTO pageDTO, LogSearchDTO logSearchDTO) {
        MPJLambdaWrapper<OperationLog> wrapper = new MPJLambdaWrapper<>();
        wrapper
                .selectAsClass(OperationLog.class, OperationLogVO.class)
                .selectAs(User::getUsername, OperationLogVO::getUserName)
                .selectAs(User::getName, OperationLogVO::getName)
                .leftJoin(User.class,User::getId,OperationLog::getUserId)
                .between(OperationLog::getTime, logSearchDTO.getBeginTime().atTime(23, 59, 59), logSearchDTO.getEndTime().atTime(23, 59, 59))
                .orderByDesc(OperationLog::getTime);
        Page<OperationLogVO> operationLogVOPage = baseMapper.selectJoinPage(new Page<>(pageDTO.getPage(), pageDTO.getSize()), OperationLogVO.class, wrapper);
        return PageUtils.toPage(operationLogVOPage);
    }

    @Override
    public void removeLatest(Integer num) {
        baseMapper.removeLatest(num);
    }
}
